﻿using UnityEngine;
using System.Collections;

public class Enemy : MonoBehaviour {

	public float health = 100;
	GameObject player;
	public GameObject parentRoom;
	bool targetPlayer = false;
	public float damageTime = 0.5f;
	public float damage = 10;
	float hitCooldown = 0;
	GameStateManager manager;
	void Start () {
		player = GameObject.FindGameObjectWithTag ("Player");
		manager = GameObject.FindGameObjectWithTag ("StateManager").GetComponent<GameStateManager>();
		GetComponent<NavMeshAgent> ().SetDestination (new Vector3(transform.position.x, 0, transform.position.z));
		health = 100 * (manager.level + 7) / 7f;
		damage = 10 * (manager.level + 7) / 7f;
	}
	
	// Update is called once per frame
	void Update () {
		hitCooldown -= Time.deltaTime;
		if (health <= 0) {
			manager.enemiesKilled++;
			parentRoom.GetComponent<Room> ().enemies.Remove (gameObject);
			Destroy (gameObject);
		} else if (targetPlayer) {
			if(Vector3.Distance(transform.position, player.transform.position) > 2.5f) {
				GetComponent<NavMeshAgent> ().SetDestination (new Vector3(player.transform.position.x, 0, player.transform.position.z));
			}
			else {
				GetComponent<NavMeshAgent> ().SetDestination (transform.position);

				if(hitCooldown < 0) {
					hitCooldown = damageTime;
					player.GetComponent<Player>().Hurt(damage);
				}
			}
		}
	}
	public void UpdatePath() {
		targetPlayer = true;
	}
}
